<?php
App::uses('AppController', 'Controller');
/**
 * Projects Controller
 *
 * @property Project $Project
 */
class ProjectsController extends AppController {


/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Project->recursive = 0;
		$this->set('projects', $this->paginate());
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Project->id = $id;
		if (!$this->Project->exists()) {
			throw new NotFoundException(__('Invalid project'));
		}
		if(!$this->Project->userAccess($this->Session->read('Auth.User'),'project_id',$id,'ProjectsUser',$this->Auth->user('group_id'))){
			throw new NotFoundException(__('You do not have permission to access this page'));
		}
		

		
		$this->loadModel("Server");
		$this->loadModel("Serversip");
		$servers = $this->Server->find('list');
		$serversips =$this->Serversip->find('list');
		$project = $this->Project->read(null, $id);
		if(isset($project['Domain']) && is_array($project['Domain']) && !empty($project['Domain'])){
			foreach ($project['Domain'] as $key => $value) {
				$project['Domain'][$key]['server_name'] = (isset($servers[$value['server_id']])) ? $servers[$value['server_id']] : 'Server Id '.$value['server_id'];
				$project['Domain'][$key]['server_ip_value'] = (isset($serversips[$value['serversip_id']])) ? $serversips[$value['serversip_id']] : 'Server IP '.$value['serversip_id'];
			}
		}
		
		if(isset($this->passedArgs['p']) && !empty($this->passedArgs['p']) && $this->passedArgs['p'] == "metas"){
			$this->Project->recursive = -1;
			$options['joins'] = array(
					array('table' => 'domains_projects',
							'alias' => 'DomainsProject',
							'type' => 'inner',
							'conditions' => array(
									'Project.id = DomainsProject.project_id'
							)
					),
					array('table' => 'domains',
							'alias' => 'Domain',
							'type' => 'inner',
							'conditions' => array(
									'DomainsProject.domain_id = Domain.id'
							)
					),
					array('table' => 'configurations',
							'alias' => 'Configuration',
							'type' => 'inner',
							'conditions' => array(
									'Domain.id = Configuration.domain_id'
							)
					),
					array('table' => 'uploads',
							'alias' => 'Upload',
							'type' => 'left',
							'conditions' => array(
									'Upload.id = Configuration.upload_id'
							)
					)
			);
			$options['conditions'] = array(
					'Project.id' => $id,
					'Configuration.name'=>'CustomMetaTags'
			);
			
			$options['fields'] = array(
					'Configuration.id',
					'Configuration.name',
					'Configuration.value',
					'Upload.name',
					'Domain.name','Domain.id',
					'Project.id',
					'Project.name',
					'Project.domain'
			);
			
			$metas = $this->Project->find('all',$options);
// 			debug($metas);
		}
		
		$this->set(compact('project','metas'));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Project->create();
			if ($this->Project->save($this->request->data)) {
				$this->Session->setFlash(__('The project has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project could not be saved. Please, try again.'));
			}
		}
		$users = $this->Project->User->find('list');
		$domains = $this->Project->Domain->find('list');
		$this->set(compact('domains','users'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Project->id = $id;
		if (!$this->Project->exists()) {
			throw new NotFoundException(__('Invalid project'));
		}
		
		if(!$this->Project->userAccess($this->Session->read('Auth.User'),'project_id',$id,'ProjectsUser')){
			throw new NotFoundException(__('You do not have permission to access this page'));
		}
		
		
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Project->save($this->request->data)) {
				$this->Session->setFlash(__('The project has been saved'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The project could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->Project->read(null, $id);
		}
		$users = $this->Project->User->find('list');
		$domains = $this->Project->Domain->find('list');
		$this->set(compact('domains','users'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Project->id = $id;
		if (!$this->Project->exists()) {
			throw new NotFoundException(__('Invalid project'));
		}
		
		if(!$this->Project->userAccess($this->Session->read('Auth.User'),'project_id',$id,'ProjectsUser')){
			throw new NotFoundException(__('You do not have permission to access this page'));
		}
		if ($this->Project->delete()) {
			$this->Session->setFlash(__('Project deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Project was not deleted, Please delete first all domains in project'));
		$this->redirect(array('action' => 'index'));
	}
}
